//
//  CommonWkWebVC.m
//  XianjinXia
//
//  Created by Xianjinkd on 2017/7/3.
//  Copyright © 2017年 lxw. All rights reserved.
//

#import "CommonWkWebVC.h"
#import "CommonWebView.h"
#import "DSUtils.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"
#import "NSString+NSURL.h"
#import "NSString+Additions.h"
#import "UserManager.h"
#import "UIDeviceHardware.h"
#import "NSString+GetParam.h"
#import "WKScriptMessageHandler.h"
#import "ApplicationUtil.h"
#import "NSString+NSURL.h"

@interface CommonWkWebVC ()<WkWebViewDelegate>
@property (nonatomic, strong) UIBarButtonItem   * closeButtonItem;
@property (nonatomic, strong) UIBarButtonItem   * backItem;
@property (nonatomic, strong) UIBarButtonItem   * refreshItem;
@property (nonatomic, strong) UIBarButtonItem   * spaceButtonItem;
@property (nonatomic, strong) CommonWebView     * myWebView;
@property (nonatomic, strong) NSDictionary      * dict;//返回数据
@property (nonatomic, strong) NSMutableURLRequest *requestMessage;
@property (nonatomic, strong) UIBarButtonItem   * allReadbtn;
@property (nonatomic, strong) NSDictionary      * dictJS;
@property (nonatomic, strong) NSString          * timeIn;
@property (nonatomic, assign) int               navStyle;
@property (nonatomic, strong) NSString          * originUrl;
@property (nonatomic, strong) UIButton          * btnClose;

@property (nonatomic, strong) NSString          * titleString;//用于埋点
@end

@implementation CommonWkWebVC

#pragma mark - VC生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.myWebView.wkWebView.configuration.userContentController addScriptMessageHandler:[WKScriptMessageHandler scriptWithDelegate:self.myWebView] name:@"Native"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.myWebView.wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"Native"];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc{
    //delegate 置为nil
    //删除通知
}

#pragma mark - View创建与设置




#pragma mark - 初始化数据源

#pragma mark - Delegate
#pragma mark ---------wkwebview代理
- (void)handJS:(WKScriptMessage *)message {
    if ([message.name isEqualToString:@"Native"]) {
        if ([message.body isKindOfClass:[NSDictionary class]]) {
            NSDictionary * parm = message.body;
            
        }
    }
}

- (NSDictionary *)getParmWithArr:(NSArray *)arr {
    NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity:0];
    for (NSDictionary * tmpDict in arr) {
        [dict setObject:tmpDict[@"value"] forKey:tmpDict[@"key"]];
    }
    return dict;
}



- (void)wkwebViewDidStartLoad:(CommonWebView *)webview {
    NSLog(@"页面开始加载");
}

- (bool)wkwebView:(CommonWebView *)webview shouldStartLoadWithURL:(NSString *)URL {
    NSLog(@"截取到URL：%@",URL);
    return YES;
}

- (void)wkwebView:(CommonWebView *)webview didFinishLoadingURL:(NSString *)URL {
    NSLog(@"页面加载完成");
    if ([DSUtils isBlankString:self.myWebView.wkWebView.title]) {
//        [self.customNavBar setTitle:self.myWebView.wkWebView.title];
    }
    [self updateNavigationItems];
}


- (void)wkwebView:(CommonWebView *)webview didFailToLoadURL:(NSString *)URL error:(NSError *)error {
    NSLog(@"加载出现错误");
}


#pragma mark - Other
//关闭H5页面，直接回到原生页面
- (void)popNactive {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)popVC{
    if ([self.myWebView.wkWebView canGoBack]) {
        [self.myWebView.wkWebView goBack];
        [self.myWebView.wkWebView reload];
    }else {
        [self popNactive];
    }
}

- (void)updateNavigationItems {
    UIBarButtonItem *spaceButtonItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceButtonItem1.width = 5;
    if (self.myWebView.wkWebView.canGoBack) {
        [self.btnClose setHidden:NO];
    }else{
        [self.btnClose setHidden:YES];
    }
}

-(UIBarButtonItem*)closeButtonItem{
    if (!_closeButtonItem) {
        _closeButtonItem = [self getNavItemWithTitle:@"关闭" withSel:@selector(popNactive)];
    }
    return _closeButtonItem;
}

- (UIBarButtonItem *)refreshItem {
    if (!_refreshItem) {
        _refreshItem = [self getNavItemWithTitle:@"刷新" withSel:@selector(refresh)];
    }
    return _refreshItem;
}

- (UIBarButtonItem *)getNavItemWithTitle:(NSString *)title withSel:(SEL)action{
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action];
    [item setTintColor:[UIColor blackColor]];
    return item;
}

- (UIBarButtonItem *)backItem {
    if (!_backItem) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [btn setTitle:@"" forState:UIControlStateNormal];
        [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [btn setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
        [btn addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:17]];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        [btn sizeToFit];
//        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btn.frame = CGRectMake(-5, 0, 25, 40);
        _backItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    }
    return _backItem;
}

- (UIBarButtonItem *)spaceButtonItem {
    if (!_spaceButtonItem) {
        _spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        _spaceButtonItem.width = -5;
    }
    return _spaceButtonItem;
}





@end
